package com.controller.frontdesk;

import com.bean.frontdesk.bo.AddMsgBO;
import com.bean.frontdesk.vo.*;
import com.bean.vo.admin.Result;
import com.google.gson.Gson;
import com.service.frontdesk.GoodServiceImplement;
import com.service.frontdesk.UserServiceImplement;
import com.sun.xml.internal.bind.v2.model.core.ID;
import com.utils.GsonUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

@WebServlet("/api/mall/goods/*")
public class ShowGoodsServlet extends HttpServlet {
    private Gson gson = GsonUtils.getGson();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/mall/goods/", "");
        if ("getGoodsByType".equals(action)){
            showAllGoodsByType(request,response);
        }
        if ("searchGoods".equals(action)){

        showGoodsBySearch(request,response);
        }
        if ("getGoodsInfo".equals(action)){
            showGoodsDetails(request,response);
        }
        if ("getGoodsMsg".equals(action)){
            showMsg(request,response);
        }
        if ("getGoodsComment".equals(action)){
            showEvaluation(request,response);
        }

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/mall/goods/", "");
        if ("askGoodsMsg".equals(action)){
            askGoodsMsg(request,response);
        }
    }


    /**
     * 提交问答
     * @param request
     * @param response
     */
    private void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        AddMsgBO addMsgBO = gson.fromJson(requestString, AddMsgBO.class);
        GoodServiceImplement goodServiceImplement = new GoodServiceImplement();
        int addState = goodServiceImplement.addMsgByAddMsgBO(addMsgBO);
        if (addState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"评论失败")));


    }

    /**
     * 展示评价
     * @param request
     * @param response
     */
    private void showEvaluation(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String goodsIdString = request.getParameter("goodsId");
        int goodsId =Integer.parseInt(goodsIdString);

        GoodServiceImplement goodServiceImplement = new GoodServiceImplement();
        GoodCommentVO goodCommentVO = goodServiceImplement.showGoodComment(goodsId);
        response.getWriter().println(gson.toJson(new Result(0,goodCommentVO,null)));

    }


    /**
     * 展示问答
     * @param request
     * @param response
     */
    private void showMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        int id = Integer.parseInt(idString);
        GoodServiceImplement goodServiceImplement = new GoodServiceImplement();
        List<MsgVO> msgVOS = goodServiceImplement.showMsg(id);
        response.getWriter().println(gson.toJson(new Result(0,msgVOS,null)));

    }


    /**
     * 显示商品详情
     * @param request
     * @param response
     */
    private void showGoodsDetails(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        int id = Integer.parseInt(idString);
        GoodServiceImplement goodServiceImplement = new GoodServiceImplement();
        GoodsDetailVO goodsDetailVO = goodServiceImplement.showGoodsDetails(id);
        response.getWriter().println(gson.toJson(new Result(0,goodsDetailVO,null)));
    }


    /**
     * 搜索商品
     * @param request
     * @param response
     */
    private void showGoodsBySearch(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String keywordString = request.getParameter("keyword");
        GoodServiceImplement goodServiceImplement = new GoodServiceImplement();
        List<SelectVO> selectVOS = goodServiceImplement.searchGoodsLikeName(keywordString);
        response.getWriter().println(gson.toJson(new Result(0,selectVOS,null)));
    }

    /**
     * 根据分类展示商品
     * @param request
     * @param response
     */
    private void showAllGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeIdString = request.getParameter("typeId");
        int typeId = Integer.parseInt(typeIdString);
        GoodServiceImplement goodServiceImplement = new GoodServiceImplement();
        List<ShowGoodsVO> showGoodsVOS = goodServiceImplement.showAllGoodsByTypeId(typeId);
        response.getWriter().println(gson.toJson(new Result(0,showGoodsVOS,null)));

    }

}
